"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A1检测项确保Database的最大连接数配置正确检测与修复成功
Create At   : 2024/10/23
Owner       : lihongji
Description :
    1.数据库testse_1、testse_2后检测A1项
    2.修复A1不符合安全规范项目后再次检测
    3.设置testse_1、testse_2连接数分别为800、2000后检测A1项
    4.修复A1不符合安全规范项目后再次检测
Expect      :
    1.打印告警信息
    2.检测打印信息中不在包含关于datconnlimit配置的警告信息
    3.设置成功并打印告警信息
    4.检测打印信息中不在包含关于datconnlimit配置的警告信息
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):
        step = ('----step1:数据库testse_1、testse_2后检测A1项 '
                'expect:打印告警信息----')
        self.log.info(step)
        check_sql = ("drop database if exists testse_1;"
                     "drop database if exists testse_2;"
                     "create database testse_1;"
                     "create database testse_2;")
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A1 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_res,
                      '执行失败' + step)
        self.assertIn("Ensure the maximum connection settings for the "
                      "database are configured correctly.",
                      check_res, '执行失败' + step)

        step = ('----step2:修复A1不符合安全规范项目后再次检测 '
                'expect:检测打印信息中不在包含关于datconnlimit配置的警告信息----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B1 --detail;'
                          f'gs_checkse -i A1 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure the maximum connection settings for "
                         "the database are configured correctly.", check_res,
                         '执行失败' + step)

        step = ('----step3:设置testse_1、testse_2连接数分别为800、2000后检测A1项 '
                'expect:设置成功并打印告警信息----')
        self.log.info(step)
        check_sql = ("""UPDATE pg_database SET datconnlimit=800 
        WHERE datname='testse_1';UPDATE pg_database SET datconnlimit=2000 
        WHERE datname='testse_2';""")
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        gs_checkse_cmd = (f'source {self.env_path};gs_checkse -i A1 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG, create_res,
                      '执行失败' + step)
        self.assertIn("Ensure the maximum connection settings for "
                      "the database are configured correctly.", check_res,
                      '执行失败' + step)

        step = ('----step4:修复A1不符合安全项目后再次检测 '
                'expect:检测打印信息中不在包含关于datconnlimit配置的警告信息----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B1 --detail;'
                          f'gs_checkse -i A1 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure the maximum connection settings for "
                         "the database are configured correctly.",
                         check_res, '执行失败' + step)

    def tearDown(self):
        step = ('----step5:需清理环境    expect:成功----')
        self.log.info(step)
        drop_sql = ("drop database if exists testse_1;"
                    "drop database if exists testse_2;")
        create_res = self.sh_primary.execut_db_sql(drop_sql)
        self.log.info(create_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, create_res,
                      '执行失败' + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')